package com.mine.crawler.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class InitializeCrawlerApplication
{
	private static Logger log = Logger.getLogger(InitializeCrawlerApplication.class);
	
	public static Properties InitConfig;
	public static Properties EnvConfig;
	public static Properties Log4J;
	public static Properties EnvSpecificConfig; //Info about location
	public static Properties MySqlDBConfig;
	//public static Properties FinalConfig;
	
	public static String InitConfigFileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator + Constants.CONFIG_FILE_NAME;
	public static String EnvConfigFileName =  Constants.PROP_FOLDER_LOCATION + 
					File.separator + Constants.ENVIRONMENT_FILENAME;
	public static String LogConfigFileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator+Constants.LOG4J_FILENAME;
	public static String MySqlDBLogConfigFileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator+Constants.MySqlDB_FILENAME;


	public static String EnvConfig_OFFICE_FileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator+Constants.ENV_OFFICE_FILENAME;
	public static String EnvConfig_HOME_ZLAPPY_FileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator+Constants.ENV_HOME_ZLAPPY_FILENAME;
	public static String EnvConfig_HOME_ELAPPY_FileName = Constants.PROP_FOLDER_LOCATION + 
					File.separator+Constants.ENV_HOME_ELAPPY_FILENAME;

	public static String Env_Location;
	public static String EnvConfig_LOCAL_FILENAME;
	
	public static Properties getInitConfig()
	{		
		if(InitConfig == null)
			Initialize();
		return InitConfig;
	}
	
	private static void PreInitialize()
	{
		
	}
	
	public static void Initialize()
	{
		PreInitialize();
		Log4JInit();
		InitConfig = ConfigLoader.LoadFromFile(InitConfigFileName);
		EnvConfig = ConfigLoader.LoadFromFile(EnvConfigFileName);
		MySqlDBConfig = ConfigLoader.LoadFromFile(MySqlDBLogConfigFileName);
		EnvSpecificInit();
		EnvSpecificConfig = ConfigLoader.LoadFromFile(EnvConfig_LOCAL_FILENAME);
		PostInitialize();
	}
	
	private static void Log4JInit()
	{
		Log4J = new Properties();
		try
		{
			Log4J.load(new FileInputStream(LogConfigFileName) );
			PropertyConfigurator.configure(Log4J);
			log.debug("Test");
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static void EnvSpecificInit()
	{
		Env_Location = EnvConfig.getProperty("env");
		if(Env_Location.equalsIgnoreCase("office"))
			EnvConfig_LOCAL_FILENAME = EnvConfig_OFFICE_FileName;
		else if (Env_Location.equalsIgnoreCase("homeZ-Lappy"))
			EnvConfig_LOCAL_FILENAME = EnvConfig_HOME_ZLAPPY_FileName;
		else if (Env_Location.equalsIgnoreCase("homeE-Lappy"))
			EnvConfig_LOCAL_FILENAME = EnvConfig_HOME_ELAPPY_FileName;
		//System.out.println("EnvConfig_LOCAL_FILENAME = "+EnvConfig_LOCAL_FILENAME);
	}
	private static void PostInitialize()
	{
		EnSpConfOverridingInitConf();
		//PrintPropertiesList();
	}
	
	private static void EnSpConfOverridingInitConf()
	{
		Set<Object> keySet = EnvSpecificConfig.keySet();
		Iterator<Object> it = keySet.iterator();
		String currentKey;
		String currentVal;
		
		while(it.hasNext())
		{
			currentKey = (String) it.next();
			currentVal = EnvSpecificConfig.getProperty(currentKey);
			if(currentVal != null)
				InitConfig.setProperty(currentKey, currentVal);
		}
	}
	public static void PrintPropertiesList()
	{
		if(InitConfig != null)
		{
			Set<Object> keySet = InitConfig.keySet();
			Iterator<Object> it = keySet.iterator();
			String currentKey;
			
			while(it.hasNext())
			{
				currentKey = (String)it.next();
				System.out.println("[ "+currentKey+" ] = "
						+InitConfig.getProperty(currentKey));
			}
			//System.out.println(InitConfig);
		}
	}	
	
	public static void PrintProperties(Properties prop)
	{
		if(prop != null)
		{
			Set<Object> keySet = prop.keySet();
			Iterator<Object> it = keySet.iterator();
			String currentKey;
			
			while(it.hasNext())
			{
				currentKey = (String)it.next();
				System.out.println("[ "+currentKey+" ] = "
						+prop.getProperty(currentKey));
			}
			//System.out.println(prop);
		}
		
	}
}
